﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
    [Serializable()]
    public class DSBenhNhanBO : Csla.BusinessBase<DSBenhNhanBO>
    {
        #region Business Properties

        private int? _BenhNhanID = null;
        public int? BenhNhanID
        {
            get { return this._BenhNhanID; }
            set { this._BenhNhanID = value; }
        }

        private string _SoLuuTru = null;
        public string SoLuuTru
        {
            get { return this._SoLuuTru; }
            set { this._SoLuuTru = value; }
        }

        private string _MaBenhNhan = null;
        public string MaBenhNhan
        {
            get { return this._MaBenhNhan; }
            set { this._MaBenhNhan = value; }
        }

        private string _HoTen = null;
        public string HoTen
        {
            get { return this._HoTen; }
            set { this._HoTen = value; }
        }

        private DateTime? _NgaySinh = null;
        public DateTime? NgaySinh
        {
            get { return this._NgaySinh; }
            set { this._NgaySinh = value; }
        }

        private bool? _GTNu = false;
        public bool? GTNu
        {
            get { return this._GTNu; }
            set { this._GTNu = value; }
        }

        private int? _DoiTuongID = null;
        public int? DoiTuongID
        {
            get { return this._DoiTuongID; }
            set { this._DoiTuongID = value; }
        }

        private DateTime? _CDNgay = null;
        public DateTime? CDNgay
        {
            get { return this._CDNgay; }
            set { this._CDNgay = value; }
        }

        private int? _CDBenhChinhID = null;
        public int? CDBenhChinhID
        {
            get { return this._CDBenhChinhID; }
            set { this._CDBenhChinhID = value; }
        }

        private string _CDBenhKemTheoIDList = null;
        public string CDBenhKemTheoIDList
        {
            get { return this._CDBenhKemTheoIDList; }
            set { this._CDBenhKemTheoIDList = value; }
        }

        private string _CDBienChungIDList = null;
        public string CDBienChungIDList
        {
            get { return this._CDBienChungIDList; }
            set { this._CDBienChungIDList = value; }
        }

        private string _CDPhanBiet = null;
        public string CDPhanBiet
        {
            get { return this._CDPhanBiet; }
            set { this._CDPhanBiet = value; }
        }

        private string _KLTaiKham = null;
        public string KLTaiKham
        {
            get { return this._KLTaiKham; }
            set { this._KLTaiKham = value; }
        }

        private string _KLLoiDan = null;
        public string KLLoiDan
        {
            get { return this._KLLoiDan; }
            set { this._KLLoiDan = value; }
        }

        private int? _KLKhoaID = null;
        public int? KLKhoaID
        {
            get { return this._KLKhoaID; }
            set { this._KLKhoaID = value; }
        }

        private DateTime? _KLNgayVaoVien = null;
        public DateTime? KLNgayVaoVien
        {
            get { return this._KLNgayVaoVien; }
            set { this._KLNgayVaoVien = value; }
        }

        private int? _BuongID = null;
        public int? BuongID
        {
            get { return this._BuongID; }
            set { this._BuongID = value; }
        }

        private int? _GiuongID = null;
        public int? GiuongID
        {
            get { return this._GiuongID; }
            set { this._GiuongID = value; }
        }

        private int? _SoNgayNamVien = null;
        public int? SoNgayNamVien
        {
            get { return this._SoNgayNamVien; }
            set { this._SoNgayNamVien = value; }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _BenhNhanID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public void SetIsChild()
        {
            MarkAsChild();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newBenhNhanID = -1;
        private DSBenhNhanBO()
        {
            _BenhNhanID = newBenhNhanID--;
        }

        public DSBenhNhanBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public DSBenhNhanBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static DSBenhNhanBO NewRootDSBenhNhanBO()
        {
            return DataPortal.Create<DSBenhNhanBO>(new RootCriteria());
        }

        internal static DSBenhNhanBO NewChildDSBenhNhanBO()
        {
            return DataPortal.Create<DSBenhNhanBO>(new ChildCriteria());
        }

        public static DSBenhNhanBO GetRootDSBenhNhanBO(int _BenhNhanID)
        {
            return DataPortal.Fetch<DSBenhNhanBO>(new RootCriteria(_BenhNhanID));
        }

        public static DSBenhNhanBO GetRootDSBenhNhanBOByWhere(string where)
        {
            return DataPortal.Fetch<DSBenhNhanBO>(where);
        }

        internal static DSBenhNhanBO GetChildDSBenhNhanBO(SqlDataReader dataReader)
        {
            return new DSBenhNhanBO(dataReader);
        }

        public static void DeleteDSBenhNhanBO(int _BenhNhanID)
        {
            DataPortal.Delete(new RootCriteria(_BenhNhanID));
        }

        public static void DeleteDSBenhNhanBO(int _BenhNhanID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_BenhNhanID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int BenhNhanID;
            public bool directDelete = false;
            public RootCriteria(int _BenhNhanID)
            {
                this.BenhNhanID = _BenhNhanID;
                this.directDelete = false;
            }

            public RootCriteria(int _BenhNhanID, bool directDelete)
            {
                this.BenhNhanID = _BenhNhanID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int BenhNhanID;
            public bool directDelete = false;
            public ChildCriteria(int _BenhNhanID)
            {
                this.BenhNhanID = _BenhNhanID;
                this.directDelete = false;
            }

            public ChildCriteria(int _BenhNhanID, bool directDelete)
            {
                this.BenhNhanID = _BenhNhanID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DSBenhNhanBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[BenhNhanID] = " + criteria.BenhNhanID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DSBenhNhanBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["BenhNhanID"] is DBNull))
                this._BenhNhanID = (int)dataReader["BenhNhanID"];
            if (!(dataReader["SoLuuTru"] is DBNull))
                this._SoLuuTru = (string)dataReader["SoLuuTru"];
            if (!(dataReader["MaBenhNhan"] is DBNull))
                this._MaBenhNhan = (string)dataReader["MaBenhNhan"];
            if (!(dataReader["HoTen"] is DBNull))
                this._HoTen = (string)dataReader["HoTen"];
            if (!(dataReader["NgaySinh"] is DBNull))
                this._NgaySinh = (DateTime)dataReader["NgaySinh"];
            if (!(dataReader["GTNu"] is DBNull))
                this._GTNu = (bool)dataReader["GTNu"];
            if (!(dataReader["DoiTuongID"] is DBNull))
                this._DoiTuongID = (int)dataReader["DoiTuongID"];
            if (!(dataReader["CDNgay"] is DBNull))
                this._CDNgay = (DateTime)dataReader["CDNgay"];
            if (!(dataReader["CDBenhChinhID"] is DBNull))
                this._CDBenhChinhID = (int)dataReader["CDBenhChinhID"];
            if (!(dataReader["CDBenhKemTheoIDList"] is DBNull))
                this._CDBenhKemTheoIDList = (string)dataReader["CDBenhKemTheoIDList"];
            if (!(dataReader["CDBienChungIDList"] is DBNull))
                this._CDBienChungIDList = (string)dataReader["CDBienChungIDList"];
            if (!(dataReader["CDPhanBiet"] is DBNull))
                this._CDPhanBiet = (string)dataReader["CDPhanBiet"];
            if (!(dataReader["KLTaiKham"] is DBNull))
                this._KLTaiKham = (string)dataReader["KLTaiKham"];
            if (!(dataReader["KLLoiDan"] is DBNull))
                this._KLLoiDan = (string)dataReader["KLLoiDan"];
            if (!(dataReader["KLKhoaID"] is DBNull))
                this._KLKhoaID = (int)dataReader["KLKhoaID"];
            if (!(dataReader["KLNgayVaoVien"] is DBNull))
                this._KLNgayVaoVien = (DateTime)dataReader["KLNgayVaoVien"];
            if (!(dataReader["BuongID"] is DBNull))
                this._BuongID = (int)dataReader["BuongID"];
            if (!(dataReader["GiuongID"] is DBNull))
                this._GiuongID = (int)dataReader["GiuongID"];

            if (this._KLNgayVaoVien == null)
                this._SoNgayNamVien = 0;
            else
                this._SoNgayNamVien = (int)DateTime.Now.Subtract(this._KLNgayVaoVien.Value).TotalDays;
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_BenhNhanID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_BenhNhanID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
